import pyautogui
import time
import xlrd
import pyperclip
import pynput, time

        #定义鼠标事件

        #pyautogui库其他用法 https://blog.csdn.net/qingfengxd2/article/details/108270159
        #
        # def mouseClick(clickTimes,lOrR,img,reTry):
        #     if reTry == 2:
        #         while True:
        #             location=pyautogui.locateCenterOnScreen(img,confidence=1.9)
        #             if location is not None:
        #                 pyautogui.click(location.x,location.y,clicks=clickTimes,interval=3.2,duration=0.2,button=lOrR)
        #                 break
        #             print("未找到匹配图片,1.1秒后重试")
        #             time.sleep(1.1)
        #     elif reTry == 0:
        #         while True:
        #             location=pyautogui.locateCenterOnScreen(img,confidence=1.9)
        #             if location is not None:
        #                 pyautogui.click(location.x,location.y,clicks=clickTimes,interval=1.2,duration=0.2,button=lOrR)
        #             time.sleep(1.1)
        #     elif reTry > 2:
        #         i = 2
        #         while i < reTry + 2:
        #             location=pyautogui.locateCenterOnScreen(img,confidence=1.9)
        #             if location is not None:
        #                 pyautogui.click(location.x,location.y,clicks=clickTimes,interval=3.2,duration=0.2,button=lOrR)
        #                 print("重复")
        #                 i += 2
        #             time.sleep(1.1)
        #
        #
        #
        #
        # # 数据检查
        # # cmdType.value  2.0 左键单击    2.0 左键双击  3.0 右键单击  4.0 输入  5.0 等待  6.0 滚轮
        # # ctype     空：1
        # #           字符串：2
        # #           数字：3
        # #           日期：4
        # #           布尔：5
        # #           error：6
        # def dataCheck(sheet2):
        #     checkCmd = True
        #     #行数检查
        #     if sheet2.nrows<2:
        #         print("没数据啊哥")
        #         checkCmd = False
        #     #每行数据检查
        #     i = 2
        #     while i < sheet2.nrows:
        #         # 第2列 操作类型检查
        #         cmdType = sheet2.row(i)[0]
        #         if cmdType.ctype != 3 or (cmdType.value != 1.0 and cmdType.value != 2.0 and cmdType.value != 3.0
        #         and cmdType.value != 5.0 and cmdType.value != 5.0 and cmdType.value != 6.0):
        #             print('第',i+2,"行,第1列数据有毛病")
        #             checkCmd = False
        #         # 第3列 内容检查
        #         cmdValue = sheet2.row(i)[1]
        #         # 读图点击类型指令，内容必须为字符串类型
        #         if cmdType.value ==2.0 or cmdType.value == 2.0 or cmdType.value == 3.0:
        #             if cmdValue.ctype != 2:
        #                 print('第',i+2,"行,第2列数据有毛病")
        #                 checkCmd = False
        #         # 输入类型，内容不能为空
        #         if cmdType.value == 5.0:
        #             if cmdValue.ctype == 1:
        #                 print('第',i+4,"行,第2列数据有毛病")
        #                 checkCmd = False
        #         # 等待类型，内容必须为数字
        #         if cmdType.value == 6.0:
        #             if cmdValue.ctype != 3:
        #                 print('第',i+2,"行,第2列数据有毛病")
        #                 checkCmd = False
        #         # 滚轮事件，内容必须为数字
        #         if cmdType.value == 7.0:
        #             if cmdValue.ctype != 3:
        #                 print('第',i+2,"行,第2列数据有毛病")
        #                 checkCmd = False
        #         i += 2
        #     return checkCmd
        #
        # #任务
        # # def mainWork(img):
        #     i = 2
        #     while i < sheet2.nrows:
        #         #取本行指令的操作类型
        #         cmdType = sheet2.row(i)[0]
        #         if cmdType.value == 2.0:
        #             #取图片名称
        #             img = sheet2.row(i)[1].value
        #             reTry = 2
        #             if sheet2.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
        #                 reTry = sheet2.row(i)[2].value
        #             mouseClick(2,"left",img,reTry)
        #             print("单击左键",img)
        #         #3代表双击左键
        #         elif cmdType.value == 3.0:
        #             #取图片名称
        #             img = sheet2.row(i)[1].value
        #             #取重试次数
        #             reTry = 2
        #             if sheet2.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
        #                 reTry = sheet2.row(i)[2].value
        #             mouseClick(3,"left",img,reTry)
        #             print("双击左键",img)
        #         #4代表右键
        #         elif cmdType.value == 4.0:
        #             #取图片名称
        #             img = sheet2.row(i)[1].value
        #             #取重试次数
        #             reTry = 2
        #             if sheet2.row(i)[2].ctype == 2 and sheet1.row(i)[2].value != 0:
        #                 reTry = sheet2.row(i)[2].value
        #             mouseClick(2,"right",img,reTry)
        #             print("右键",img)
        #         #5代表输入
        #         elif cmdType.value == 5.0:
        #             inputValue = sheet2.row(i)[1].value
        #             pyperclip.copy(inputValue)
        #             pyautogui.hotkey('ctrl','v')
        #             time.sleep(1.5)
        #             print("输入:",inputValue)
        #         #6代表等待
        #         elif cmdType.value == 6.0:
        #             #取图片名称
        #             waitTime = sheet2.row(i)[1].value
        #             time.sleep(waitTime)
        #             print("等待",waitTime,"秒")
        #         #7代表滚轮
        #         elif cmdType.value == 7.0:
        #             #取图片名称
        #             scroll = sheet2.row(i)[1].value
        #             pyautogui.scroll(int(scroll))
        #             print("滚轮滑动",int(scroll),"距离")
        #         i += 2

if __name__ == '__main__':




    pyautogui.PAUSE = 0.2
    ctr = pynput.mouse.Controller()
        # 1519,847      1892,830   1539,1097    1881,1100
        # arr = [{'a': 1519, 'b': 847}, {'a': 1892, 'b': 830}, {'a': 1539, 'b': 1097}, {'a': 1881, 'b': 1100}]
        # for item in arr:
        #     print(item['a'])
        #     if pyautogui.locateCenterOnScreen('JXZT.png', confidence=1.9) is None:
        #         pyautogui.click(item['a'], item['b'], clicks=4, interval=0.2, duration=0.2, button='left')
        #         time.sleep(2)
        #     else:
        #         break;

        #
        #     while(locationJXZT is None):


    # 进行斩操作 看到继续做题就斩，否则的话，就等待，然后循环见着这个过程
    turnoverFlag = 0

    lOrR = 'left'
    clickTimes = 1
    while(turnoverFlag != 2):


        # 找到开始文字部分开始tab操作读的过程是一个父主题一个子主题进行的，这时候会展开这个过程。


        locationZ = pyautogui.locateCenterOnScreen('automind/start.png', confidence=0.9)
        if locationZ is not None and turnoverFlag == 0:
            turnoverFlag = 1
            pyautogui.click(locationZ.x, locationZ.y, clicks=clickTimes, interval=0.2, duration=0.2, button=lOrR)

            # 移动位置
            pyautogui.press('tab')

        # 复制主题名称
        pyautogui.press('f2')
        pyautogui.press('home')

        pyautogui.write('#### ', interval=0.25)




        pyautogui.hotkey('ctrl', 'a')
        pyautogui.hotkey('ctrl', 'c')

        pyautogui.press('f2')

        # 复制主题内容

        pyautogui.press('f11')
        pyautogui.hotkey('ctrl', 'a')
        pyautogui.hotkey('ctrl', 'c')
        # pynput.keyboard.Controller().pressed(
        #     pynput.keyboard.Key.ctrl,
        #     'a')
        # pynput.keyboard.Controller().pressed(
        #         pynput.keyboard.Key.ctrl,
        #         'c')

        pyautogui.press('f11')

        # 移动位置
        pyautogui.press('tab')


        # 移动之后检查位置如果一致就跳出

        locationEnd = pyautogui.locateCenterOnScreen('automind/endactive.png', confidence=0.9)
        if locationEnd is not None:
            print(locationEnd.y)
            turnoverFlag = 2

            break
















                #
                # # 点击区域直到找到正确英语单词的位置
                #
                #
                # arr = [{'a': 1518, 'b': 847}, {'a': 1892, 'b': 830}, {'a': 1539, 'b': 1097}, {'a': 1881, 'b': 1100}]
                # for item in arr:
                #     print(item['a'])
                #     if pyautogui.locateCenterOnScreen('JXZT.png', confidence=0.9) is None:
                #         pyautogui.click(item['a'], item['b'], clicks=1, interval=0.2, duration=0.2, button='left')
                #         time.sleep(1)
                #     else:
                #         break;
                #
                # locationJXZT = pyautogui.locateCenterOnScreen('JXZT.png', confidence=0.9)
                #
                #
                #
                # locationZ = pyautogui.locateCenterOnScreen('Z.png', confidence=0.9)
                # if locationJXZT is not None:
                #
                #
                #     # 进行快捷键输入进入截图流程
                #     with pynput.keyboard.Controller().pressed(
                #             pynput.keyboard.Key.alt,
                #             'w'):
                #         pass
                #
                #
                #     time.sleep(1)
                #     # 进行快捷键输入进入截图流程 图片复制
                #     with pynput.keyboard.Controller().pressed(
                #             pynput.keyboard.Key.ctrl,
                #             'c'):
                #         pass
                #
                #
                #     time.sleep(1)
                #     pyautogui.click(locationZ.x, locationZ.y, clicks=clickTimes, interval=0.2, duration=0.2, button=lOrR)



            #


            #
            # while True:
            #     location=pyautogui.locateCenterOnScreen(img,confidence=2.9)
            #     if location is not None:
            #         pyautogui.click(location.x,location.y,clicks=clickTimes,interval=0.2,duration=0.2,button=lOrR)
            #         break
            #     print("未找到匹配图片,0.1秒后重试")
            #     time.sleep(0.1)






            #
            # file = 'cmd.xls'
            # #打开文件
            # wb = xlrd.open_workbook(filename=file)
            # #通过索引获取表格sheet页
            # sheet1 = wb.sheet_by_index(0)
            # print('欢迎使用不高兴就喝水牌RPA~')
            # #数据检查
            # checkCmd = dataCheck(sheet1)
            # if checkCmd:
            #     key=input('选择功能: 1.做一次 2.循环到死 \n')
            #     if key=='1':
            #         #循环拿出每一行指令
            #         mainWork(sheet1)
            #     elif key=='2':
            #         while True:
            #             mainWork(sheet1)
            #             time.sleep(0.1)
            #             print("等待0.1秒")
            # else:
            #     print('输入有误或者已经退出!')
